package xyz.cerberu.cet.anime.controller;


import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.cerberu.cet.anime.domain.Anime;
import xyz.cerberu.cet.anime.service.AnimeManagerServiceImpl;
import xyz.cerberu.cet.anime.service.AnimeServiceImpl;
import xyz.cerberu.cet.global.annotation.JsonFilter;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.global.domain.Page;
import xyz.cerberu.cet.global.domain.Result;
import xyz.cerberu.cet.global.utils.ResultUtils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * @author β世界
 * Created on 9:01
 * @Description 动漫管理Control (需要权限为admin和root的用户才可以使用)
 */
@RequiresUser
@RequiresRoles(value = {"admin","root"},logical = Logical.OR)
@Controller
@RequestMapping("/manager/animemanager")
public class AnimeManagerControl {

    private final AnimeServiceImpl animeServiceImpl;
    private final AnimeManagerServiceImpl animeManagerServiceImpl;
    private final Global global;

    @Autowired
    public AnimeManagerControl(AnimeServiceImpl animeServiceImpl, AnimeManagerServiceImpl animeManagerServiceImpl, Global global) {
        this.animeServiceImpl = animeServiceImpl;
        this.animeManagerServiceImpl = animeManagerServiceImpl;
        this.global = global;
    }

    /** 获取动漫这个类所有的公开字段 **/
    private static final Field[] FIELDS = Anime.class.getDeclaredFields();

    /**
     * @author β世界 on 11:20 2020/9/26
     * 获取指定条件下的所有动漫数据
     * @param pageNumber: 当前页码
     * @param year: 年份
     * @param quarter: 季度
     * @return com.Cet.common.domain.Result
     **/
    @JsonFilter(type = Anime.class,filter = "cover,introducation,quarter,week,year")
    @ResponseBody
    @RequestMapping(value = "/filterAnimes",method = RequestMethod.GET)
    public Result<Page<Anime>> filterAnimes(int pageNumber,
                               int year,
                               @RequestParam(required = false,defaultValue = "") String quarter){
        List<Anime> anime = animeManagerServiceImpl.getAllAnime(pageNumber,year,quarter,0);
        Page<Anime> page = Page.build(anime,pageNumber,global.getPageShowNumber());
        return ResultUtils.genSuccessResult(page);
    }

    /**
     * @author β世界 on 22:14 2020/10/10
     * 进行模糊查询来查询
     * @param search: 搜索信息
     * @param pageNumber: 当前页
     * @return com.Cet.common.domain.Result
     **/
    @JsonFilter(type = Anime.class,filter = "cover,introducation,quarter,week,year")
    @ResponseBody
    @RequestMapping(value = "/searchAnime",method = RequestMethod.POST)
    public Result<Page<Anime>> searchAnime(String search,int pageNumber){
        Page<Anime> page = Page.build(animeManagerServiceImpl.fuzzySearch(search),pageNumber, global.getPageShowNumber());
        return ResultUtils.genSuccessResult(page);
    }

    /**
     * @author β世界 on 11:22 2020/9/26
     * 需要权限为：root
     * 更新一个动漫信息 (必须是root用户才可以更改这个)
     * 需要使用一个过滤器来过滤传递过来的属性(目前在control进行处理)
     * @param anime: 动漫实体
     * @return com.Cet.common.domain.Result
     **/
    @RequiresRoles(value = {"root"})
    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<Object> updateAnime(Anime anime){
        try {
            // 检查传递过来的属性
            if (!matchRule(anime)){
                // 当属性不被通行,记录异常操作,并且返回
                return ResultUtils.genErrorResult(400,"字段不正确");
            }
            // 更新动漫信息
            animeManagerServiceImpl.updateAnime(anime);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtils.genErrorResult(500,"更新失败");
        }
        return ResultUtils.genSuccessResult("更新成功");
    }

    @ResponseBody
    @RequestMapping(value = "/getAnimeFullInfo",method = RequestMethod.GET)
    public Result getAnimeFullInfo(String ani_id){
        return ResultUtils.genSuccessResult(animeServiceImpl.animeAbstract(ani_id));
    }

    /**
     * @author β世界 on 22:18 2020/10/13
     * 检查是否符合放行规则
     * @param anime: 动漫实体
     * @return boolean
     **/
    private boolean matchRule(Anime anime) throws IllegalAccessException {
        // 获取允许被修改字段的名字
        List<String> propertyNames = Arrays.asList(global.getAnimeReleaseField());
        // 遍历所有字段,检查传递过来的对象中，允许放行的字段是否有值
        for(Field field : FIELDS){
            // 设置允许越过访问权限修改和获取属性值
            field.setAccessible(true);
            // 获取字段值
            Object value = field.get(anime);
            // 判断值是否为空，为空就不管，如果不为空，那就检查是否存在于放行列表中，没有就返回false
            if(value != null && !Integer.valueOf(0).equals(value) && !propertyNames.contains(field.getName())){
                return false;
            }
        }
        return true;
    }

}
